1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module soup.WebsocketConnection;
26 
27 private import gio.IOStream;
28 private import glib.Bytes;
29 private import glib.ConstructionException;
30 private import glib.ErrorG;
31 private import glib.ListG;
32 private import glib.Str;
33 private import glib.c.functions;
34 private import gobject.ObjectG;
35 private import gobject.Signals;
36 private import soup.URI;
37 private import soup.WebsocketExtension;
38 private import soup.c.functions;
39 public  import soup.c.types;
40 private import std.algorithm;
41 
42 
43 /**
44  * A class representing a WebSocket connection.
45  *
46  * Since: 2.50
47  */
48 public class WebsocketConnection : ObjectG
49 {
50 	/** the main Gtk struct */
51 	protected SoupWebsocketConnection* soupWebsocketConnection;
52 
53 	/** Get the main Gtk struct */
54 	public SoupWebsocketConnection* getWebsocketConnectionStruct(bool transferOwnership = false)
55 	{
56 		if (transferOwnership)
57 			ownedRef = false;
58 		return soupWebsocketConnection;
59 	}
60 
61 	/** the main Gtk struct as a void* */
62 	protected override void* getStruct()
63 	{
64 		return cast(void*)soupWebsocketConnection;
65 	}
66 
67 	/**
68 	 * Sets our main struct and passes it to the parent class.
69 	 */
70 	public this (SoupWebsocketConnection* soupWebsocketConnection, bool ownedRef = false)
71 	{
72 		this.soupWebsocketConnection = soupWebsocketConnection;
73 		super(cast(GObject*)soupWebsocketConnection, ownedRef);
74 	}
75 
76 
77 	/** */
78 	public static GType getType()
79 	{
80 		return soup_websocket_connection_get_type();
81 	}
82 
83 	/**
84 	 * Creates a #SoupWebsocketConnection on @stream. This should be
85 	 * called after completing the handshake to begin using the WebSocket
86 	 * protocol.
87 	 *
88 	 * Params:
89 	 *     stream = a #GIOStream connected to the WebSocket server
90 	 *     uri = the URI of the connection
91 	 *     type = the type of connection (client/side)
92 	 *     origin = the Origin of the client
93 	 *     protocol = the subprotocol in use
94 	 *
95 	 * Returns: a new #SoupWebsocketConnection
96 	 *
97 	 * Since: 2.50
98 	 *
99 	 * Throws: ConstructionException GTK+ fails to create the object.
100 	 */
101 	public this(IOStream stream, URI uri, SoupWebsocketConnectionType type, string origin, string protocol)
102 	{
103 		auto __p = soup_websocket_connection_new((stream is null) ? null : stream.getIOStreamStruct(), (uri is null) ? null : uri.getURIStruct(), type, Str.toStringz(origin), Str.toStringz(protocol));
104 
105 		if(__p is null)
106 		{
107 			throw new ConstructionException("null returned by new");
108 		}
109 
110 		this(cast(SoupWebsocketConnection*) __p, true);
111 	}
112 
113 	/**
114 	 * Creates a #SoupWebsocketConnection on @stream with the given active @extensions.
115 	 * This should be called after completing the handshake to begin using the WebSocket
116 	 * protocol.
117 	 *
118 	 * Params:
119 	 *     stream = a #GIOStream connected to the WebSocket server
120 	 *     uri = the URI of the connection
121 	 *     type = the type of connection (client/side)
122 	 *     origin = the Origin of the client
123 	 *     protocol = the subprotocol in use
124 	 *     extensions = a #GList of #SoupWebsocketExtension objects
125 	 *
126 	 * Returns: a new #SoupWebsocketConnection
127 	 *
128 	 * Since: 2.68
129 	 *
130 	 * Throws: ConstructionException GTK+ fails to create the object.
131 	 */
132 	public this(IOStream stream, URI uri, SoupWebsocketConnectionType type, string origin, string protocol, ListG extensions)
133 	{
134 		auto __p = soup_websocket_connection_new_with_extensions((stream is null) ? null : stream.getIOStreamStruct(), (uri is null) ? null : uri.getURIStruct(), type, Str.toStringz(origin), Str.toStringz(protocol), (extensions is null) ? null : extensions.getListGStruct());
135 
136 		if(__p is null)
137 		{
138 			throw new ConstructionException("null returned by new_with_extensions");
139 		}
140 
141 		this(cast(SoupWebsocketConnection*) __p, true);
142 	}
143 
144 	/**
145 	 * Close the connection in an orderly fashion.
146 	 *
147 	 * Note that until the #SoupWebsocketConnection::closed signal fires, the connection
148 	 * is not yet completely closed. The close message is not even sent until the
149 	 * main loop runs.
150 	 *
151 	 * The @code and @data are sent to the peer along with the close request.
152 	 * If @code is %SOUP_WEBSOCKET_CLOSE_NO_STATUS a close message with no body
153 	 * (without code and data) is sent.
154 	 * Note that the @data must be UTF-8 valid.
155 	 *
156 	 * Params:
157 	 *     code = close code
158 	 *     data = close data
159 	 *
160 	 * Since: 2.50
161 	 */
162 	public void close(ushort code, string data)
163 	{
164 		soup_websocket_connection_close(soupWebsocketConnection, code, Str.toStringz(data));
165 	}
166 
167 	/**
168 	 * Get the close code received from the WebSocket peer.
169 	 *
170 	 * This only becomes valid once the WebSocket is in the
171 	 * %SOUP_WEBSOCKET_STATE_CLOSED state. The value will often be in the
172 	 * #SoupWebsocketCloseCode enumeration, but may also be an application
173 	 * defined close code.
174 	 *
175 	 * Returns: the close code or zero.
176 	 *
177 	 * Since: 2.50
178 	 */
179 	public ushort getCloseCode()
180 	{
181 		return soup_websocket_connection_get_close_code(soupWebsocketConnection);
182 	}
183 
184 	/**
185 	 * Get the close data received from the WebSocket peer.
186 	 *
187 	 * This only becomes valid once the WebSocket is in the
188 	 * %SOUP_WEBSOCKET_STATE_CLOSED state. The data may be freed once
189 	 * the main loop is run, so copy it if you need to keep it around.
190 	 *
191 	 * Returns: the close data or %NULL
192 	 *
193 	 * Since: 2.50
194 	 */
195 	public string getCloseData()
196 	{
197 		return Str.toString(soup_websocket_connection_get_close_data(soupWebsocketConnection));
198 	}
199 
200 	/**
201 	 * Get the connection type (client/server) of the connection.
202 	 *
203 	 * Returns: the connection type
204 	 *
205 	 * Since: 2.50
206 	 */
207 	public SoupWebsocketConnectionType getConnectionType()
208 	{
209 		return soup_websocket_connection_get_connection_type(soupWebsocketConnection);
210 	}
211 
212 	/**
213 	 * Get the extensions chosen via negotiation with the peer.
214 	 *
215 	 * Returns: a #GList of #SoupWebsocketExtension objects
216 	 *
217 	 * Since: 2.68
218 	 */
219 	public ListG getExtensions()
220 	{
221 		auto __p = soup_websocket_connection_get_extensions(soupWebsocketConnection);
222 
223 		if(__p is null)
224 		{
225 			return null;
226 		}
227 
228 		return new ListG(cast(GList*) __p);
229 	}
230 
231 	/**
232 	 * Get the I/O stream the WebSocket is communicating over.
233 	 *
234 	 * Returns: the WebSocket's I/O stream.
235 	 *
236 	 * Since: 2.50
237 	 */
238 	public IOStream getIoStream()
239 	{
240 		auto __p = soup_websocket_connection_get_io_stream(soupWebsocketConnection);
241 
242 		if(__p is null)
243 		{
244 			return null;
245 		}
246 
247 		return ObjectG.getDObject!(IOStream)(cast(GIOStream*) __p);
248 	}
249 
250 	/**
251 	 * Gets the keepalive interval in seconds or 0 if disabled.
252 	 *
253 	 * Returns: the keepalive interval.
254 	 *
255 	 * Since: 2.58
256 	 */
257 	public uint getKeepaliveInterval()
258 	{
259 		return soup_websocket_connection_get_keepalive_interval(soupWebsocketConnection);
260 	}
261 
262 	/**
263 	 * Gets the maximum payload size allowed for incoming packets.
264 	 *
265 	 * Returns: the maximum payload size.
266 	 *
267 	 * Since: 2.56
268 	 */
269 	public ulong getMaxIncomingPayloadSize()
270 	{
271 		return soup_websocket_connection_get_max_incoming_payload_size(soupWebsocketConnection);
272 	}
273 
274 	/**
275 	 * Get the origin of the WebSocket.
276 	 *
277 	 * Returns: the origin, or %NULL
278 	 *
279 	 * Since: 2.50
280 	 */
281 	public string getOrigin()
282 	{
283 		return Str.toString(soup_websocket_connection_get_origin(soupWebsocketConnection));
284 	}
285 
286 	/**
287 	 * Get the protocol chosen via negotiation with the peer.
288 	 *
289 	 * Returns: the chosen protocol, or %NULL
290 	 *
291 	 * Since: 2.50
292 	 */
293 	public string getProtocol()
294 	{
295 		return Str.toString(soup_websocket_connection_get_protocol(soupWebsocketConnection));
296 	}
297 
298 	/**
299 	 * Get the current state of the WebSocket.
300 	 *
301 	 * Returns: the state
302 	 *
303 	 * Since: 2.50
304 	 */
305 	public SoupWebsocketState getState()
306 	{
307 		return soup_websocket_connection_get_state(soupWebsocketConnection);
308 	}
309 
310 	/**
311 	 * Get the URI of the WebSocket.
312 	 *
313 	 * For servers this represents the address of the WebSocket, and
314 	 * for clients it is the address connected to.
315 	 *
316 	 * Returns: the URI
317 	 *
318 	 * Since: 2.50
319 	 */
320 	public URI getUri()
321 	{
322 		auto __p = soup_websocket_connection_get_uri(soupWebsocketConnection);
323 
324 		if(__p is null)
325 		{
326 			return null;
327 		}
328 
329 		return ObjectG.getDObject!(URI)(cast(SoupURI*) __p);
330 	}
331 
332 	/**
333 	 * Send a binary message to the peer. If @length is 0, @data may be %NULL.
334 	 *
335 	 * The message is queued to be sent and will be sent when the main loop
336 	 * is run.
337 	 *
338 	 * Params:
339 	 *     data = the message contents
340 	 *
341 	 * Since: 2.50
342 	 */
343 	public void sendBinary(ubyte[] data)
344 	{
345 		soup_websocket_connection_send_binary(soupWebsocketConnection, data.ptr, cast(size_t)data.length);
346 	}
347 
348 	/**
349 	 * Send a message of the given @type to the peer. Note that this method,
350 	 * allows to send text messages containing %NULL characters.
351 	 *
352 	 * The message is queued to be sent and will be sent when the main loop
353 	 * is run.
354 	 *
355 	 * Params:
356 	 *     type = the type of message contents
357 	 *     message = the message data as #GBytes
358 	 *
359 	 * Since: 2.68
360 	 */
361 	public void sendMessage(SoupWebsocketDataType type, Bytes message)
362 	{
363 		soup_websocket_connection_send_message(soupWebsocketConnection, type, (message is null) ? null : message.getBytesStruct());
364 	}
365 
366 	/**
367 	 * Send a %NULL-terminated text (UTF-8) message to the peer. If you need
368 	 * to send text messages containing %NULL characters use
369 	 * soup_websocket_connection_send_message() instead.
370 	 *
371 	 * The message is queued to be sent and will be sent when the main loop
372 	 * is run.
373 	 *
374 	 * Params:
375 	 *     text = the message contents
376 	 *
377 	 * Since: 2.50
378 	 */
379 	public void sendText(string text)
380 	{
381 		soup_websocket_connection_send_text(soupWebsocketConnection, Str.toStringz(text));
382 	}
383 
384 	/**
385 	 * Sets the interval in seconds on when to send a ping message which will serve
386 	 * as a keepalive message. If set to 0 the keepalive message is disabled.
387 	 *
388 	 * Params:
389 	 *     interval = the interval to send a ping message or 0 to disable it
390 	 *
391 	 * Since: 2.58
392 	 */
393 	public void setKeepaliveInterval(uint interval)
394 	{
395 		soup_websocket_connection_set_keepalive_interval(soupWebsocketConnection, interval);
396 	}
397 
398 	/**
399 	 * Sets the maximum payload size allowed for incoming packets. It
400 	 * does not limit the outgoing packet size.
401 	 *
402 	 * Params:
403 	 *     maxIncomingPayloadSize = the maximum payload size
404 	 *
405 	 * Since: 2.56
406 	 */
407 	public void setMaxIncomingPayloadSize(ulong maxIncomingPayloadSize)
408 	{
409 		soup_websocket_connection_set_max_incoming_payload_size(soupWebsocketConnection, maxIncomingPayloadSize);
410 	}
411 
412 	/**
413 	 * Emitted when the connection has completely closed, either
414 	 * due to an orderly close from the peer, one initiated via
415 	 * soup_websocket_connection_close() or a fatal error
416 	 * condition that caused a close.
417 	 *
418 	 * This signal will be emitted once.
419 	 *
420 	 * Since: 2.50
421 	 */
422 	gulong addOnClosed(void delegate(WebsocketConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
423 	{
424 		return Signals.connect(this, "closed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
425 	}
426 
427 	/**
428 	 * This signal will be emitted during an orderly close.
429 	 *
430 	 * Since: 2.50
431 	 */
432 	gulong addOnClosing(void delegate(WebsocketConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
433 	{
434 		return Signals.connect(this, "closing", dlg, connectFlags ^ ConnectFlags.SWAPPED);
435 	}
436 
437 	/**
438 	 * Emitted when an error occurred on the WebSocket. This may
439 	 * be fired multiple times. Fatal errors will be followed by
440 	 * the #SoupWebsocketConnection::closed signal being emitted.
441 	 *
442 	 * Params:
443 	 *     error = the error that occured
444 	 *
445 	 * Since: 2.50
446 	 */
447 	gulong addOnError(void delegate(ErrorG, WebsocketConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
448 	{
449 		return Signals.connect(this, "error", dlg, connectFlags ^ ConnectFlags.SWAPPED);
450 	}
451 
452 	/**
453 	 * Emitted when we receive a message from the peer.
454 	 *
455 	 * As a convenience, the @message data will always be
456 	 * NUL-terminated, but the NUL byte will not be included in
457 	 * the length count.
458 	 *
459 	 * Params:
460 	 *     type = the type of message contents
461 	 *     message = the message data
462 	 *
463 	 * Since: 2.50
464 	 */
465 	gulong addOnMessage(void delegate(int, Bytes, WebsocketConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
466 	{
467 		return Signals.connect(this, "message", dlg, connectFlags ^ ConnectFlags.SWAPPED);
468 	}
469 
470 	/**
471 	 * Emitted when we receive a Pong frame (solicited or
472 	 * unsolicited) from the peer.
473 	 *
474 	 * As a convenience, the @message data will always be
475 	 * NUL-terminated, but the NUL byte will not be included in
476 	 * the length count.
477 	 *
478 	 * Params:
479 	 *     message = the application data (if any)
480 	 *
481 	 * Since: 2.60
482 	 */
483 	gulong addOnPong(void delegate(Bytes, WebsocketConnection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
484 	{
485 		return Signals.connect(this, "pong", dlg, connectFlags ^ ConnectFlags.SWAPPED);
486 	}
487 }